package com.example.service.user.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.example.exception.db.UpdateException;
import com.example.mapper.UserMapper;
import com.example.pojo.User;
import com.example.service.user.UserAccountHelpService;
import com.example.dto.request.user.VerificationCodeMailRequestDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author LeXin Huang
 * @date 2021年03月22日 11:00
 */
@Slf4j
@Service
public class UserAccountHelpServiceImpl extends ServiceImpl<UserMapper, User> implements UserAccountHelpService {

    @Value("${spring.mail.username}")
    private String senderMailAddress;

    private final JavaMailSenderImpl javaMailSender;

    private final TemplateEngine templateEngine;

    public UserAccountHelpServiceImpl(JavaMailSenderImpl javaMailSender, TemplateEngine templateEngine) {
        this.javaMailSender = javaMailSender;
        this.templateEngine = templateEngine;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void resetPassword(String username, String password) {
        if(!SqlHelper.retBool(this.baseMapper.updatePassword(username, password))) {
            throw new UpdateException("修改密码失败!");
        }
    }

    @Override
    public void sendVerificationCodeMail(VerificationCodeMailRequestDTO mailInfo) throws MessagingException, MailException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        addCc(mimeMessage);
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        mimeMessageHelper.setSubject(mailInfo.getSubject());
        mimeMessageHelper.setFrom(senderMailAddress);
        mimeMessageHelper.setTo(mailInfo.getRecipientAddress());
        mimeMessageHelper.setText(getMailContent(mailInfo), true);
        javaMailSender.send(mimeMessage);
    }

    private void addCc(MimeMessage mimeMessage) throws MessagingException {
        mimeMessage.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(senderMailAddress));
    }

    private String getMailContent(VerificationCodeMailRequestDTO mailInfo) {
        Context context = new Context();
        render(context, mailInfo.getRecipientName(), mailInfo.getCode(), mailInfo.getTimeOut(), mailInfo.getTimeUnitName());
        String emailPageTemplateName = "verificationMailTemplate";
        return templateEngine.process(emailPageTemplateName, context);
    }

    private void render(Context context, String username, String verificationCode, Long timOut, String timeUnitName) {
        context.setVariable("username", username);
        context.setVariable("verificationCode", verificationCode);
        context.setVariable("timeOut", timOut);
        context.setVariable("timeUnit", timeUnitName);
    }

}
